import request from '../utils/request';

export async function voteSearch({page = 1, page_size = 10}) {
  return request('/api/vote/search?page_num=' + page + '&page_size=' + page_size + '&debug=1');
}

export async function voteDetail({id}) {
  return request('/api/vote/detail?id=' + id + '&debug=1');
}

export async function subjectSearch({page = 1, page_size = 100}) {
  return request('/api/vote/subject/search?page=' + page + '&page_size=' + page_size + '&debug=1');
}

export async function candidateSearch({page = 1, page_size = 100}) {
  return request('/api/vote/candidate/search?page=' + page + '&page_size=' + page_size + '&debug=1');
}

export async function saveVote(query) {
  var startTime = query.start_end_time[0].format('x');
  var endTime = query.start_end_time[1].format('x');


  var form = new FormData;
  form.append('id', query.id);
  form.append('subject_id', query.subject);
  form.append('candidate_ids', query.candidate.join(','));
  form.append('rule', query.rule);
  form.append('permission', query.permission ? 1 : 0);
  form.append('frequency', query.frequency);
  form.append('result_visibility', query.result_visibility ? 1 : 0);
  form.append('template', query.template);
  form.append('start_time', parseInt(startTime / 1000));
  form.append('end_time', parseInt(endTime / 1000));

  return request('/api/vote/save?debug=1', {
    method: 'post',
    body: form
  });
}

export async function createVote(query) {
  var startTime = query.start_end_time[0].format('x');
  var endTime = query.start_end_time[1].format('x');


  var form = new FormData;
  form.append('subject_id', query.subject);
  form.append('candidate_ids', query.candidate.join(','));
  form.append('rule', query.rule);
  form.append('permission', query.permission ? 1 : 0);
  form.append('frequency', query.frequency);
  form.append('result_visibility', query.result_visibility ? 1 : 0);
  form.append('template', query.template);
  form.append('start_time', parseInt(startTime / 1000));
  form.append('end_time', parseInt(endTime / 1000));

  return request('/api/vote/create?debug=1', {
    method: 'post',
    body: form
  });
}

export async function removeSubject({id}) {
  var form = new FormData;

  form.append('id', id);

  return request('/api/vote/subject/remove?debug=1', {
    method: 'post',
    body: form
  });
}

export async function createSubject({title, description}) {
  var form = new FormData;

  form.append('title', title);
  form.append('description', description);

  return request('/api/vote/subject/create?debug=1', {
    method: 'post',
    body: form
  });
}

export async function removeCandidate({id}) {
  var form = new FormData;

  form.append('id', id);

  return request('/api/vote/candidate/remove?debug=1', {
    method: 'post',
    body: form
  });
}

export async function createCandidate({name, description, images}) {
  var form = new FormData;

  form.append('name', name);
  form.append('description', description);
  form.append('images', JSON.stringify(images));

  return request('/api/vote/candidate/create?debug=1', {
    method: 'post',
    body: form
  });
}

export async function submitVote({id, candidates}) {
  var form = new FormData;

  form.append('id', id);
  form.append('candidates', candidates);

  return request('/api/vote/submit?debug=1', {
    method: 'post',
    body: form
  });
}
